package com.rowenci.iot_agriculture.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.rowenci.iot_agriculture.bean.Greenhouse;
import com.rowenci.iot_agriculture.bean.iot.Com;
import com.rowenci.iot_agriculture.bean.iot.Message;
import com.rowenci.iot_agriculture.service.api.ComService;
import com.rowenci.iot_agriculture.service.api.GreenhouseService;
import com.rowenci.iot_agriculture.service.api.MessageService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping(value = "/greenhouse")
public class GreenhouseController {

    @Resource
    private GreenhouseService greenhouseService;
    @Resource
    private MessageService messageService;
    @Resource
    private ComService comService;

    @RequestMapping(value = "/show_greenhouse")
    public String show_greenhouse(Model model) throws Exception {

        //首先更新大棚信息
        List<Greenhouse> greenhouses_old = greenhouseService.findAll();
        for (int i = 0; i < greenhouses_old.size(); i++) {
            //获取第i个大棚
            Integer gid = greenhouses_old.get(i).getId();
            //获取第i个大棚的所有com(最多4个 除了reader)
            List<Com> coms = comService.findAllByHouse_idExceptReader(gid);
            //当大棚的coms不为空时遍历coms列表并从message中获取信息格式化并直接存入greenhouse中
            if (!coms.isEmpty()) {
                for (int j = 0; j < coms.size(); j++) {
                    //获取串口
                    Com com = coms.get(j);
                    //获取message
                    Message message = messageService.findMessage(com.getId());
                    //格式化数据
                    /**
                     * 传感器类型
                     * reader 阅读器
                     * illuminance 光照传感器
                     * temphumi(tempature/humidity) 温湿度传感器
                     */
                    switch (com.getSensor()) {
                        case "illuminance": {
                            String info = message.getInfo();
                            System.out.println(info);
                            greenhouseService.updateIlluminanceByHouse_id(gid, Integer.parseInt(info.substring(0, 2)));
                        }
                        case "temphumi": {
                            String info = message.getInfo();
                            if (!info.isEmpty()) {
                                //格式化数据
                                int humidity_1 = Integer.parseInt(info.substring(14, 16), 16); //HH
                                int humidity_2 = Integer.parseInt(info.substring(16, 18), 16); //HL
                                Integer humidity = (humidity_1 * 256 + humidity_2) / 10;
                                int tempature_1 = Integer.parseInt(info.substring(18, 20), 16); //TH
                                int tempature_2 = Integer.parseInt(info.substring(20, 22), 16); //TL
                                Integer tempature = (tempature_1 * 256 + tempature_2) / 10;
                                greenhouseService.updateTemphumiByHouse_id(gid, tempature, humidity);
                            }
                        }
                    }
                }
            }
        }
        List<Greenhouse> greenhouses_new = greenhouseService.findAll();
        model.addAttribute("greenhouses", greenhouses_new);
        return "/pages/console/greenhouselist";
    }

    @PostMapping("/editVegetable")
    @ResponseBody
    public String editVegetable(Integer id, String vegetable, Model model) throws Exception {
        try {
            greenhouseService.updateVegetableByHouse_id(id, vegetable);
        } catch (Exception e) {
            model.addAttribute("msg", "fault");
            return JSON.toJSONString(model);
        }
        model.addAttribute("msg", "success");
        return JSON.toJSONString(model);
    }

    @PostMapping("/addGreenHouse")
    @ResponseBody
    public String addGreenHouse(Greenhouse greenhouse, Model model) throws Exception {
        try {
            greenhouseService.addGreenHouse(greenhouse);
        } catch (Exception e) {
            model.addAttribute("msg", "fault");
            return JSON.toJSONString(model, SerializerFeature.IgnoreNonFieldGetter);
        }
        model.addAttribute("msg", "success");
        return JSON.toJSONString(model, SerializerFeature.IgnoreNonFieldGetter);
    }

    @PostMapping("/delGreenHouse")
    @ResponseBody
    public String delGreenHouse(Integer house_id, Model model) throws Exception {
        if (greenhouseService.delByHouse_id(house_id) && comService.delComByHouse_id(house_id)) {
            model.addAttribute("msg", "success");
        } else {
            model.addAttribute("msg", "fault");
        }
        return JSON.toJSONString(model, SerializerFeature.IgnoreNonFieldGetter);
    }

}
